package com.jmb.api.common.utils;

import com.jmb.api.common.constant.RedisConstant;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class UserInfoUtil {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public Object getUserToken(HttpServletRequest request)  {

        String userId = request.getHeader("UserId");
        String key = RedisConstant.USER_LOGIN_KEY+userId;
        return redisTemplate.opsForValue().get(key);
    }

    // 获取用户的真实姓名
    public String getRealName(HttpServletRequest request) {
        HashMap<String,Object> userToken = (HashMap<String, Object>) getUserToken(request);
        Object username = userToken.get("username");

        return username.toString();
    }
    // 获取用户的id
    public Integer getUserId(HttpServletRequest request) {
        HashMap<String,Object> userToken = (HashMap<String, Object>) getUserToken(request);
        Object userId = userToken.get("userId");
        return userId.hashCode();
    }
}
